<template>
	<view>
		<navbar name="账户设置"></navbar>
		<view class="w750bgf">
			<view class="user-view">
				<view class="user-title">
					头像
				</view>
				<!-- @tap="camera"  -->
				<button class="avatar" open-type="chooseAvatar" @chooseavatar="chooseAvatar" plain>
					<image class="user-image" :src="avatarUrl" mode="头像"></image>
				</button>
			</view>
			<u-line />
			<view class="user-view">
				<view class="user-title">
					昵称
				</view>
				<input class="user-input" type="nickname" :value="nickName" placeholder="请输入昵称" @input="nameBlur" />
			</view>
			<u-line />
		</view>
		<button class="quit" type="default" @tap="updateUser(avatarUrl, sex, nickName, remark)">保存</button>
	</view>
</template>
<script>
import uLine from "@/uview-ui/components/u-line/u-line.vue"
import { updateUserInfo } from '@/sg/index'

export default {
	components: {
		uLine,
	},
	data() {
		return {
			value1: '',
			label1: '',
			sex: 1, //
			avatarUrl: '', //头像
			nickName: '', //用户昵称
			remark: '',
			result: {},
			jumpInterface: '', //登录成功后跳转到界面
			transferParameters: {}, //传递的参数
		}
	},
	created() {
		let _this = this;
		// 监听从裁剪页发布的事件，获得裁剪结果
		uni.$on('uAvatarCropper', this.uAvatarCropper)
	},
	onLoad(e) {
		if (e.jumpInterface) {
			this.jumpInterface = e.jumpInterface;
		}
		if (e.transferParameters) {
			this.transferParameters = JSON.parse(e.transferParameters);
		}
		this.getBaseUserInfo()
	},
	onUnload() {
		uni.$off('uAvatarCropper', this.uAvatarCropper)
	},
	methods: {
		// 裁剪图片回调
		uAvatarCropper(path) {
			let _this = this;
			let loginKey = global.IS_DEV ? 'USER_TOKEN_BSY_T' : 'USER_TOKEN_BSY_Z'

			uni.uploadFile({
				url: global.host + 'cms-service/file',
				filePath: path,
				fileType: 'image',
				name: 'upfile',
				header: {
					'Authorization': uni.getStorageSync(loginKey),
					userSource: 1, //用户类型 1:C端用户  2:员工
				},
				success: (resdata) => {
					_this.avatarUrl = JSON.parse(resdata.data).result.url;
				},
				fail: (err) => {
					console.log('uploadImage fail', err);
				}
			});
		},
		//剪切图片
		chooseAvatar(e) {
			const { avatarUrl } = e.detail;
			this.uAvatarCropper(avatarUrl)
		},
		//修改头像
		amendHeadImg() {

		},
		// 用户输入昵称
		nameBlur(e) {
			this.nickName = e.detail.value;
		},
		// 用户输入简介
		exBlur(e) {
			this.remark = e.detail.value;
		},
		camera() {
			let _this = this;
			uni.chooseImage({
				count: 1,
				sizeType: ['original', 'compressed'],
				sourceType: ['album', 'camera'],
				success: function (res) {
					var imageSrc = res.tempFilePaths[0]
					let loginKey = global.IS_DEV ? 'USER_TOKEN_BSY_T' : 'USER_TOKEN_BSY_Z'

					uni.uploadFile({
						url: global.host + 'cms-service/file',
						filePath: imageSrc,
						fileType: 'image',
						name: 'upfile',
						header: {
							'Authorization': uni.getStorageSync(loginKey),
							userSource: 1, //用户类型 1:C端用户  2:员工
						},
						success: (resdata) => {
							_this.avatarUrl = JSON.parse(resdata.data).result.url;
						},
						fail: (err) => {
							console.log('uploadImage fail', err);
						}
					});
				}
			});
		},
		updateUser(avatarUrl, gender, nickName, remark) {
			let _this = this;
			let fdata = {
				avatarUrl: avatarUrl, //头像
				sex: gender, //性别
				nickName: nickName, //用户昵称
				userId: uni.getStorageSync(global.IS_DEV ? 'USER_DATA_BSY_T' : 'USER_DATA_BSY_Z').userId,
				remark: remark ? remark : ''
			}
			this.$loginl.put(global.apiUrls.updateUser, fdata)
				.then(res => {
					if (res.data.code == 1000) {
						uni.showToast({
							title: res.data.message,
							duration: 2000
						});
						setTimeout(() => {
							let pageurl = uni.getStorageSync('upPageurl');
							if (this.jumpInterface) {
								console.info(this.transferParameters, "this.transferParameters登录成功后的参数2")
								this.$urouter.redirectTo({
									url: this.jumpInterface,
									params: this.transferParameters
								})
								return
							}

							if (pageurl) { //默认跳回上一页
								if (pageurl != '/pagesMy/my/relevance/share' && pageurl !=
									'/pagesMy/my/interconnect/interconnect' && pageurl !=
									'/pages/signIn/signinPages' && pageurl !=
									'/pages/subpackage/home/healthData/healthData') { //签到 家人邀请
									if (uni.getStorageSync('kxjCode')) { //邀请康享家
										uni.redirectTo({
											url: '/subpackage/maker/maker'
										});
									} else {
										uni.redirectTo({
											url: '/pages/subpackage/home/oneAccess/oneAccess'
										});
									}
								} else {
									uni.redirectTo({
										url: pageurl + '?signIn=1'
									});
								}
							} else { //为空情况
								uni.reLaunch({
									url: '/pages/tab/home'
								});
							}
						}, 2000);
						this.result = Object.assign(this.result, fdata);
						updateUserInfo(this.result)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						});
					}
				}).catch(err => {
					console.error(err)
				})
		},
		getBaseUserInfo() {
			let _this = this;
			this.$loginl.get(global.apiUrls.getBaseUserInfo + '?userId=' + uni.getStorageSync(
				global.IS_DEV ? 'USER_DATA_BSY_T' : 'USER_DATA_BSY_Z').userId, {})
				.then(res => {
					if (res.data.code == 1000) {
						let result = res.data.result
						_this.avatarUrl = result.avatarUrl;
						_this.nickName = result.nickName;
						_this.remark = result.remark;
						_this.sex = result.sex;
						_this.result = result;
					}
				}).catch(err => { })
		},
		handleTap(name) {
			this.$refs[name].show()
		},
		handleChange(e) {
			console.log('change::', e)
			// this.sex = e.value
		},
		handleConfirm(e) {
			// 如果存在多个picker，可以在picker上设置dataset属性，confirm中获取，就能区分是哪个picker了
			console.log('confirm::', e)
			this.sex = e.value
		},
		handleCancel(e) {
			console.log('cancel::', e)
		}
	}

}
</script>

<style lang="scss">
@import "./base.scss"
</style>
